package com.exmaple.lemlin.ui.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.exmaple.lemlin.R;
import com.exmaple.lemlin.app.APPIM;
import com.exmaple.lemlin.app.Constants;
import com.exmaple.lemlin.entity.FriendGood;
import com.exmaple.lemlin.entity.FriendGoodDao;
import com.exmaple.lemlin.ui.abstracts.AbstractBaseFragment;
import com.exmaple.lemlin.ui.activity.ChatWindowsActivity;
import com.exmaple.lemlin.ui.activity.MainActivity;
import com.exmaple.lemlin.ui.activity.FriendActivity;
import com.exmaple.lemlin.ui.adapter.UserAdapter;
import com.exmaple.lemlin.utils.LogUtils;
import com.exmaple.lemlin.utils.ToastUtil;
import com.exmaple.lemlin.xmpp.XmppConnection;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MainFriendFragment extends AbstractBaseFragment implements MainActivity.OnMainFriendListener, UserAdapter.OnItemClickListener {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private List<FriendGood> friends = new ArrayList<>();
    private UserAdapter userAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_friend;
    }

    @Override
    protected void initView() {
        userAdapter = new UserAdapter(getActivity(), friends);
        userAdapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(userAdapter);

    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initLazyLoad() {

    }

    @Override
    public void onResume() {
        super.onResume();
        userAdapter.addData(APPIM.getDaoSession().getFriendGoodDao().queryBuilder()
                .where(FriendGoodDao.Properties.Sub.eq("both")).list());
    }

    @Override
    public void onFriendGood(FriendGood friendGood) {
        userAdapter.addItem(friendGood);
    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(getActivity(), ChatWindowsActivity.class);
        intent.putExtra(Constants.BUNDLE_KEY_USER_NAME, friends.get(position).getName());
        startActivityForResult(intent, 1);
    }

    @OnClick({R.id.lly_new_friend, R.id.lly_group_chat})
    public void onViewClicked(View view) {
        if (getActivity() == null) {
            return;
        }
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.lly_new_friend:
                intent.setClass(getActivity(), FriendActivity.class);
                startActivity(intent);
                break;
            case R.id.lly_group_chat:
//                ToastUtil.showShort(getActivity(),"功能未开放");
//                XmppConnection.getInstance().createChatRoom("chat_root", "linlimin");

                XmppConnection.getInstance().dd();
                break;
        }
    }
}
